/* $RCSFile: PageSettings.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Configuration/Settings/PageSettings.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:27  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Web.Caching;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Web;

using AddonNice.Core;
using AddonNice.Helpers;
using AddonNice.Design;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Configuration.Settings;
using AddonNice.Settings.Cache;
using AddonNice.UI.DataTypes;

using Path      =   AddonNice.Settings.Path;
using Cache2    =   AddonNice.Settings.Cache.CurrentCache;

namespace AddonNice.Configuration
{
	#region Event classes, Delegates 

    public class PageSettingsCreatedEventArgs : EventArgs
	{
        public PageSettings pageSettings;
        public PageSettingsDictionary Settings;
		
        public PageSettingsCreatedEventArgs(PageSettingsDictionary _S,PageSettings pSettings)
		{
			Settings	    =	_S;
            pageSettings    =   pSettings;
		}
	}

	public delegate void PageSettingsCreatedEventHandler(PageSettingsCreatedEventArgs e);

	#endregion

	/// <summary>
	/// PageSettings Class encapsulates the detailed settings 
	/// for a specific Page in the Portal
	/// </summary>
    public class PageSettings
    {

        /// <summary>
        ///  Roles allowed to display this page
        /// </summary>
        public string AuthorizedRoles			    =	string.Empty;
        
        /// <summary>
        /// Contained modules
        /// </summary>
        public List<ModuleConfiguration> Modules	=   new List<ModuleConfiguration>();

        /// <summary>
        /// DB Id for parent page
        /// </summary>
        public int ParentPageID;

        /// <summary>
        ///  Portal ID for this page   (could be different from actual portal for admin pages -> -2)
        /// </summary>
        public int PortalID;

        /// <summary>
        ///  DB unique id for this page
        /// </summary>
        public int PageID;

        /// <summary>
        /// Flags concerning this page: okfor menus, etc. Cf ADNPageFlags
        /// </summary>
        public ADNPageFlags Flags                       =   ADNPageFlags.Normal;
        
        /// <summary>
        ///  Display order of this page in the sitemap
        /// </summary>
        public int PageOrder;
        public int NbPanes;
        public int[] Width                              =   new int[5];

        /// <summary>
        /// Common Portals parameters 
        /// </summary>
        private PortalSettings _portalSettings          =   null;

        /// <summary>
        /// Page global parameters will be accessed as sonn as loaded
        /// initialized by PageSettings Ctor then read from file
        /// </summary>
        private PageSettingsDictionary _Settings        =   null;

        /// <summary>
        /// Real Page Name set on creation
        /// </summary>
        public string PageName                          =   string.Empty;

        /// <summary>
        /// Meta Title for page or PageName if not defined, based on current language
        /// </summary>
        public string PageTitle                         =   string.Empty;
        /// <summary>
        /// Used for displaying page name in menus, PageName if not defined, based on current language
        /// </summary>
        public string PageKeyPhrase                     =   string.Empty;

        public string PageUrlName                       =   string.Empty;

        /// <summary>
        /// Page icon
        /// </summary>
        public string PageImage                         =   string.Empty;

        /// <remarks>
        /// to have dropdown list for the themes and layout, we need the data path for the portal (for private theme and layout)
        /// we need the portalPath here for this use and it has to be set from the current portalSettings before getting the
        /// CustomSettings for a tab
        /// </remarks>
        private string portalPath                       =   null;


        #region properties
        /// <summary>
        /// Page Settings For Search Engines
        /// </summary>
        public PageSettingsDictionary Settings
        {
            get
            {
                if (SettingsTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings Settings GET PageID: {0}, is NULL: {1}", PageID, _Settings == null ), SettingsTraceSwitch.Sw.Info);
                if ( _Settings == null)
                    _Settings = GetPageCustomSettings(PageID);
                return _Settings;
            }
        }

        /// <summary>
        /// Portal virtual path, ended by '/'
        /// </summary>
        public string PortalPath
        {
            get { return portalPath; }
            set
            {
                portalPath = value;
                if (!portalPath.EndsWith("/")) 
                    portalPath += "/";
            }
        }


        /// <summary>
        /// Stores current portal settings 
        /// </summary>
        public PortalSettings portalSettings
        {
            get
            {
                if (_portalSettings == null)
                {
                    // Obtain PortalSettings from Current Context
                    if (HttpContext.Current != null)
                        _portalSettings = (PortalSettings)HttpContext.Current.Items[StStr.strPortalSettings];
                }
                return _portalSettings;
            }
            set { _portalSettings = value; }
        }


        PagesDB _pDB = null;
        PagesDB PageDB
        {
            get
            {
                if (_pDB == null)
                    _pDB = new PagesDB();
                return _pDB;
            }
        }

        #endregion properties

        #region settings

        /// <summary>
        /// Used to clear any values that should be recreated following a cache reset of portal or page
        /// </summary>
        public void Reset(bool resetCache)
        {
            if (SettingsTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings Reset BEG PageID: '{0}'", PageID),PageTraceSwitch.Sw.Info);
            AuthorizedRoles			=	string.Empty;
            Modules		            =   new List<ModuleConfiguration>();
            Flags                   =   ADNPageFlags.Normal;
            _portalSettings         =   null;
            _Settings               =   null;
            PageName                =   string.Empty;
            PageUrlName             =   string.Empty;
            PageImage               =   string.Empty;
            string wkey             =   Key.TabSettings(PageID);
            if ( resetCache && CurrentCache.Exists(wkey) )
                CurrentCache.Remove(wkey);
            if (SettingsTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings Reset END PageID: '{0}'", PageID),PageTraceSwitch.Sw.Info);
        }

        public PageSettingsDictionary GetDupPageCustomSettings()
        {
            if (SettingsTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetDupPageCustomSettings BEG PageID: {0}", PageID), SettingsTraceSwitch.Sw.Info);
            return GetPageCustomSettings(PageID);
        }

        /// <summary>
        /// The PageSettings.GetPageCustomSettings Method returns a SettingsDictionary  of
        /// custom Page specific settings from the database. This method is used by Portals to access misc Page settings.
        /// </summary>
        /// <param name="pageID"></param>
        /// <returns></returns>
        public PageSettingsDictionary GetPageCustomSettings(int wpageID)
        {
            string wkey                         =   Key.TabSettings(wpageID);
            if (CurrentCache.Exists(wkey))
            {
                if ( SettingsTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageCustomSettings BEG found in cache pageID: {0}", wpageID),SettingsTraceSwitch.Sw.Info);
                return (PageSettingsDictionary)CurrentCache.Get(wkey);
            }
            if ( SettingsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageCustomSettings BEG reading from DB pageID: {0}", wpageID),SettingsTraceSwitch.Sw.Info);
            PageSettingsDictionary baseSettings =   GetPageBaseSettings();
            baseSettings.GetPageCustomSettings(wpageID);
            if ( SettingsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageCustomSettings END pageID: {0}, count:{1}", wpageID, baseSettings.Count),SettingsTraceSwitch.Sw.Info);
            return baseSettings;
        }


        /// <summary>
        /// Read Current Page sub Pages
        /// </summary>
        public static PageStripDetailsList GetPageSettingsPagesBox(int portalID,int PageID)
        {
            if ( SettingsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageSettingsPagesBox BEG pageID: {0}, portalID: {1}", PageID, portalID),SettingsTraceSwitch.Sw.Info);
            PageStripDetailsList tabs               =   null;
            SqlDataReader result                    =   null;
            try
            {
                result                              =   new PagesDB().GetPageSettingsPages(portalID,PageID);
                tabs                                =   new PageStripDetailsList(result,string.Empty);
            }
            catch (Exception exp)
            {
                if ( SettingsTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PageSettings GetPageSettingsPagesBox pageID: {0}, ex: {1}", PageID, exp),SettingsTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if (result != null)
                    result.Close();
            }
            if ( SettingsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageSettingsPagesBox END pageID: {0}, count: {1}", PageID, tabs.Count),SettingsTraceSwitch.Sw.Info);
            return tabs;
        }

        /// <summary>
        ///     Initialise from source code default values for page settings
        /// </summary>
        /// <returns></returns>
        private PageSettingsDictionary GetPageBaseSettings()
        {
            if ( SettingsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageBaseSettings BEG at: {0}", DateTime.UtcNow),SettingsTraceSwitch.Sw.Info);

            //Define base settings
            PageSettingsDictionary _baseSettings    =   new PageSettingsDictionary();
            int _groupOrderBase;
            SettingItemGroup _Group;

            #region Navigation Settings

            _groupOrderBase                         =   (int)SettingItemGroup.NAVIGATION_SETTINGS;
            _Group                                  =   SettingItemGroup.NAVIGATION_SETTINGS;

            SettingItem TabPlaceholder              =   new SettingItem(StStr.TabPlaceholder,new BooleanDataType());
            TabPlaceholder.Group                    =   _Group;
            TabPlaceholder.Order                    =   _groupOrderBase;
            TabPlaceholder.Value                    =   StStr.strFalse;
            TabPlaceholder.EnglishName              =   "Acts as a Placeholder?";
            TabPlaceholder.Description              =   "Allows this Page to act as a navigation placeholder only.";
            TabPlaceholder.Type                     =    SettingsType.Page;
            _baseSettings.AddSetting(TabPlaceholder);
            
            SettingItem TabLink                     =   new SettingItem(StStr.TabLink,new StringDataType());
            TabLink.Group                           =   _Group;
            TabLink.Value                           =   string.Empty;
            TabLink.Order                           =   _groupOrderBase + 1;
            TabLink.EnglishName                     =   "Static Link URL";
            TabLink.Description                     =   "Allows this Page to act as a navigation link to any URL.";
            TabLink.Type                            =   SettingsType.Page;
            _baseSettings.AddSetting(TabLink);

            SettingItem TabUrlKeyword               =   new SettingItem(StStr.TabUrlKeyword,new StringDataType());
            TabUrlKeyword.Group                     =   _Group;
            TabUrlKeyword.Order                     =   _groupOrderBase + 2;
            TabUrlKeyword.EnglishName               =   "Url Keyword";
            TabUrlKeyword.Description               =   "Allows you to specify a keyword that would appear in your url.";
            TabUrlKeyword.Type                      =   SettingsType.Page;
            _baseSettings.AddSetting(TabUrlKeyword);

            SettingItem UrlPageName                 =   new SettingItem(StStr.UrlPageName,new StringDataType());
            UrlPageName.Group                       =   _Group;
            UrlPageName.Order                       =   _groupOrderBase + 3;
            UrlPageName.EnglishName                 =   "Url Page Name";
            UrlPageName.Description                 =   "This setting allows you to specify a name for this Page that will show up in the url instead of defaultModulesPage.aspx";
            UrlPageName.Type                        =   SettingsType.Page;
            _baseSettings.AddSetting(UrlPageName);

            #endregion

            #region Metadata Management

            _groupOrderBase                         =   (int)SettingItemGroup.META_SETTINGS;
            _Group                                  =   SettingItemGroup.META_SETTINGS;
            
            SettingItem PageTitle                   =   new SettingItem(StStr.PageTitle,new StringDataType());
            PageTitle.Group                         =   _Group;
            PageTitle.Order                         =   _groupOrderBase++;
            PageTitle.EnglishName                   =   "Page Title";
            PageTitle.Description                   =   "Allows you to enter a title (Shows at the top of your browser) for this specific Page. Enter something here to override the default portal wide setting.";
            PageTitle.Type                          =   SettingsType.Page;
            _baseSettings.AddSetting(PageTitle);
            
            // New IE 8 browser mode <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
            SettingItem IE8BrowserMode              =   new SettingItem(StStr.IE8BrowserMode,new StringDataType());
            IE8BrowserMode.Group                    =   _Group;
            IE8BrowserMode.Order                    =   _groupOrderBase++;
            IE8BrowserMode.Value                    =   "8";
            IE8BrowserMode.EnglishName              =   "IE 8 browser mode";
            IE8BrowserMode.Description              =   "When client browser is IE8, you could for the mode using IE=8 or IE=EmulateIE7, see http://msdn.microsoft.com/en-us/library/cc817574.aspx";
            IE8BrowserMode.Type                     =   SettingsType.Page;
            _baseSettings.AddSetting(IE8BrowserMode);

            SettingItem PageMetaKeyWords            =   new SettingItem(StStr.PageMetaKeyWords,new StringDataType());
            PageMetaKeyWords.Group                  =   _Group;
            PageMetaKeyWords.Order                  =   _groupOrderBase++;
            PageMetaKeyWords.EnglishName            =   "Page Keywords";
            PageMetaKeyWords.Description            =   "This setting is to help with search engine optimisation. Enter 1-15 Default Keywords that represent what this Page is about.Enter something here to override the default portal wide setting.";
            PageMetaKeyWords.Type                   =   SettingsType.Page;
            _baseSettings.AddSetting(PageMetaKeyWords);

            SettingItem PageMetaDescription         =   new SettingItem(StStr.PageMetaDescription,new StringDataType());
            PageMetaDescription.Group               =   _Group;
            PageMetaDescription.Order               =   _groupOrderBase++;
            PageMetaDescription.EnglishName         =   "Page Description";
            PageMetaDescription.Description         =   "This setting is to help with search engine optimisation. Enter a description (Not too long though. 1 paragraph is enough) that describes this particular Tab / Page. Enter something here to override the default portal wide setting.";
            PageMetaDescription.Type                =   SettingsType.Page;
            _baseSettings.AddSetting(PageMetaDescription);

            SettingItem PageMetaExpires             =   new SettingItem(StStr.PageMetaExpires,new StringDataType());
            PageMetaExpires.Group                   =   _Group;
            PageMetaExpires.Order                   =   _groupOrderBase++;
            PageMetaExpires.EnglishName             =   "Expires";
            PageMetaExpires.Description             =   "This setting allows you to tell when your page expires. Enter a date.";
            PageMetaExpires.Type                    =   SettingsType.Page;
            _baseSettings.AddSetting(PageMetaExpires);

            SettingItem PageMetaRobots              =   new SettingItem(StStr.PageMetaRobots,new StringDataType());
            PageMetaRobots.Group                    =   _Group;
            PageMetaRobots.Order                    =   _groupOrderBase++;
            PageMetaRobots.Value                    =   "all";
            PageMetaRobots.EnglishName              =   "Robots";
            PageMetaRobots.Description              =   "This setting allows you to tell Robots what to do. Enter something like \"all\".";
            PageMetaRobots.Type                     =   SettingsType.Page;
            _baseSettings.AddSetting(PageMetaRobots);

            // Refresh in content "seconds;url=new_url"
            SettingItem PageRefresh                 =   new SettingItem(StStr.PageMetaRefresh,new StringDataType());
            PageRefresh.Group                       =   _Group;
            PageRefresh.Order                       =   _groupOrderBase++;
            PageRefresh.EnglishName                 =   "Refresh";
            PageRefresh.Description                 =   "This setting allows to automatically refresh your page. Format to use \"seconds;url=new_url\".";
            PageRefresh.Type                        =   SettingsType.Page;
            _baseSettings.AddSetting(PageRefresh);

            // encoding  text/html;charset=UTF-8 see w3c
            SettingItem PageEncoding                =   new SettingItem(StStr.PageEncoding,new StringDataType());
            PageEncoding.Group                      =   _Group;
            PageEncoding.Order                      =   _groupOrderBase++;
            PageEncoding.EnglishName                =   "Encoding";
            PageEncoding.Description                =   "This setting is to be used carefully, let it empty by default .net will take care of everything. You can use something like: text/html;charset=UTF-8 ";
            PageEncoding.Type                       =   SettingsType.Page;
            _baseSettings.AddSetting(PageEncoding);

            SettingItem PageKeyPhrase               =   new SettingItem(StStr.PageKeyPhrase,new StringDataType());
			PageKeyPhrase.Group                     =   _Group;
			PageEncoding.Order                      =   _groupOrderBase++;
            PageKeyPhrase.EnglishName               =   "Page Keyphrase";
			PageKeyPhrase.Description               =   "This setting can be used by a module or by a control. It allows you to define a message/phrase for this particular Tab / Page This can be used for search engine optimisation. Enter something here to override the default portal wide setting.";
            PageKeyPhrase.Type                      =   SettingsType.Page;
			_baseSettings.AddSetting(PageKeyPhrase);
            #endregion

            #region ModulesLayout

            _groupOrderBase                         =   (int)SettingItemGroup.THEME_SETTINGS;
            _Group                                  =   SettingItemGroup.THEME_SETTINGS;
            
            
            ArrayList net2ThemesList                =   PageThemeManager.GetAllNet2Themes();
            NameListItem _noCustomTheme2            =   new NameListItem(string.Empty);
            net2ThemesList.Insert(0, _noCustomTheme2);

            
            SettingItem CustomTheme2                =   new SettingItem(StStr.PageCustomTheme,new CustomListDataType(net2ThemesList, StStr.Name, StStr.Name));
            CustomTheme2.Group                      =   _Group;
            CustomTheme2.Order                      =   _groupOrderBase + 1;
            CustomTheme2.EnglishName                =   "Page Custom ModulesLayout";
            CustomTheme2.Description                =   "Force a new Page specific theme.";
            CustomTheme2.Type                       =   SettingsType.Page;
            _baseSettings.AddSetting(CustomTheme2);

            // 30/1/07 CSU to manage StyleSheetTheme parameter
            // when set to true stylesheetheme is used in place of theme for pages
            SettingItem USSTheme                    =   new SettingItem(StStr.UseStyleSheetTheme,new BooleanDataType());
            USSTheme.Value                          =   StStr.strFalse;
            USSTheme.Required                       =   false;
            USSTheme.Order                          =   _groupOrderBase + 10;
            USSTheme.Group                          =   _Group;
            USSTheme.EnglishName                    =   "StyleSheetTheme";
            USSTheme.Description                    =   "Use a StyleSheetTheme in place of a theme for this page only.";
            USSTheme.Type                           =   SettingsType.Page;
            _baseSettings.AddSetting(USSTheme);

            _groupOrderBase                         =   (int)SettingItemGroup.LAYOUT_SETTINGS;
            _Group                                  =   SettingItemGroup.LAYOUT_SETTINGS;

            // get the list of available themes for modules
            ModulesLayoutManager tm                 =   new ModulesLayoutManager(portalSettings.PortalPath);
            ArrayList ModuleLayoutsList             =   tm.GetModulesLayouts();
            ModulesLayoutItem _noCustomModuleLayout =   new ModulesLayoutItem(string.Empty,ItemOrigin.None);
            ModuleLayoutsList.Insert(0, _noCustomModuleLayout);

            // get the list of available layouts
            ArrayList layoutsList                   =   new ArrayList(new PagesLayoutManager(portalSettings.PortalPath).GetLayouts());
            LayoutItem _noCustomLayout              =   new LayoutItem(string.Empty,ItemOrigin.None);
            layoutsList.Insert(0, _noCustomLayout);
            
            // get the list of available masterPages
            ArrayList MasterPagesList               =   new MasterPageManager(portalSettings.PortalPath).GetMasterPages();
            string noMasterPage                     =   LocalizeHelper.Localize("NO_MASTERPAGE","No MasterPage");
            MasterListItem _noCustomMP              =   new MasterListItem(noMasterPage,string.Empty,ItemOrigin.None);
            MasterPagesList.Insert(0, _noCustomMP);

            // modules custom layout
            SettingItem CustomModuleLayout          =   new SettingItem(StStr.PageModuleCustomLayout,new CustomListDataType(ModuleLayoutsList, StStr.Name, StStr.Name));
            CustomModuleLayout.Group                =   _Group;
            CustomModuleLayout.Order                =   _groupOrderBase + 1;
            CustomModuleLayout.EnglishName          =   "Custom Modules layout";
            CustomModuleLayout.Description          =   "Set a custom layout for the modules in this page";
            CustomModuleLayout.Type                 =   SettingsType.Page;
            _baseSettings.AddSetting(CustomModuleLayout);

            SettingItem CustomModuleLayoutAlt       =   new SettingItem(StStr.PageModuleCustomLayoutAlt,
                                                            new CustomListDataType(ModuleLayoutsList, StStr.Name, StStr.Name));
            CustomModuleLayoutAlt.Group             =   _Group;
            CustomModuleLayoutAlt.Order             =   _groupOrderBase + 2;
            CustomModuleLayoutAlt.EnglishName       =   "Custom Alt Modules layout";
            CustomModuleLayoutAlt.Description       =   "Set an alternate custom layout for the modules in this page";
            CustomModuleLayoutAlt.Type              =   SettingsType.Page;
            _baseSettings.AddSetting(CustomModuleLayoutAlt);

            // Page layout
            SettingItem CustomLayout                =   new SettingItem(StStr.CustomLayout,new CustomListDataType(layoutsList, StStr.Name, StStr.Name));
            CustomLayout.Group                      =   _Group;
            CustomLayout.Order                      =   _groupOrderBase + 3;
            CustomLayout.EnglishName                =   "Custom Page Layout";
            CustomLayout.Description                =   "Set a custom layout for this page only";
            CustomLayout.Type                       =   SettingsType.Page;
            _baseSettings.AddSetting(CustomLayout);

            // master Page for complementary pages
            SettingItem CustomLayoutMasterPage      =   new SettingItem(StStr.PageMasterPageFile,new CustomListDataType(MasterPagesList, StStr.Name, StStr.WebPath));
            CustomLayoutMasterPage.Group            =   _Group;
            CustomLayoutMasterPage.Order            =   _groupOrderBase + 4;
            CustomLayoutMasterPage.EnglishName      =   "Custom MasterPage";
            CustomLayoutMasterPage.Description      =   "Set a custom MasterPage for this page only, MasterPages are used to layout complementary pages called by modules in this page.";
            CustomLayoutMasterPage.Type             =   SettingsType.Page;
            _baseSettings.AddSetting(CustomLayoutMasterPage);


            #endregion

            #region Language/Culture Management

            _groupOrderBase                             =   (int)SettingItemGroup.CULTURE_SETTINGS;
            _Group                                      =   SettingItemGroup.CULTURE_SETTINGS;
            CultureInfo[] cultureList                   =   AddonNice.UI.WebControls.PortalModuleControl.GetLanguageList(true);

            //Localized Page title
            int counter                                 =   _groupOrderBase + 20;

            foreach (CultureInfo c in cultureList)
            {
                // '-' are not supported for resources
                string strlang                          =   c.Name.ToUpper().Replace('-', '_');
                //Ignore invariant
                if (c != CultureInfo.InvariantCulture && !_baseSettings.ContainsKey(strlang))
                {
                    SettingItem LocalizedTabKeyPhrase   =   new SettingItem(StStr.PageKeyPhrase_under + strlang,new StringDataType());
                    LocalizedTabKeyPhrase.Order         =   counter++;
                    LocalizedTabKeyPhrase.Group         =   _Group;
                    LocalizedTabKeyPhrase.EnglishName   =   "Page Key Phrase (" + c.Name + ")";
                    LocalizedTabKeyPhrase.Description   =   "Key Phrase of this Page for " + c.EnglishName + " culture.";
                    LocalizedTabKeyPhrase.Type          =   SettingsType.Page;
                    _baseSettings.AddUpper(LocalizedTabKeyPhrase.Key, LocalizedTabKeyPhrase);

                    SettingItem LocalizedTitle          =   new SettingItem(strlang,new StringDataType());
                    LocalizedTitle.Order                =   counter++;
                    LocalizedTitle.Group                =   _Group;
                    LocalizedTitle.EnglishName          =   "Title (" + c.Name + ")";
                    LocalizedTitle.Description          =   "Set title for " + c.EnglishName + " culture.";
                    LocalizedTitle.Type                 =   SettingsType.Page;
                    _baseSettings.AddSetting(LocalizedTitle);

                    SettingItem LocPageMetaKeyWords     =   new SettingItem(StStr.PageMetaKeyWords_under+strlang,new StringDataType());
                    LocPageMetaKeyWords.Group           =   _Group;
                    LocPageMetaKeyWords.Order           =   counter++;
                    LocPageMetaKeyWords.EnglishName     =   "Page Keywords (" + c.Name + ")";
                    LocPageMetaKeyWords.Description     =   "This setting is to help with search engine optimisation. Enter 1-15 Default Keywords that represent what this Page is about.Enter something here to override the default portal wide setting.";
                    LocPageMetaKeyWords.Type            =   SettingsType.Page;
                    _baseSettings.AddSetting(LocPageMetaKeyWords);

                    SettingItem LocPageMetaDescription  =   new SettingItem(StStr.PageMetaDescription_under+strlang,new StringDataType());
                    LocPageMetaDescription.Group        =   _Group;
                    LocPageMetaDescription.Order        =   counter++;
                    LocPageMetaDescription.EnglishName  =   "Page Description (" + c.Name + ")";
                    LocPageMetaDescription.Description  =   "This setting is to help with search engine optimisation. Enter a description (Not too long though. 1 paragraph is enough) that describes this particular Tab / Page. Enter something here to override the default portal wide setting.";
                    LocPageMetaDescription.Type         =   SettingsType.Page;
                    _baseSettings.AddSetting(LocPageMetaDescription);
                    
                    counter++;
                }
            }
            #endregion Language/Culture Management
            
            #region CRM settings
            _Group                                  =   SettingItemGroup.CRM_SETTINGS;

            SettingItem EntityNameSetting           =   new SettingItem(StStr.PageCRMEntityNameSettings,new StringDataType());
            EntityNameSetting.Order                 =   ((int)_Group)+1;
            EntityNameSetting.Group                 =   _Group;
            EntityNameSetting.Value                 =   string.Empty;
            EntityNameSetting.EnglishName           =   "EntityName";
            EntityNameSetting.Description           =   "Name of CRM entity associated to this page (leave blank if no entity associated).";
            EntityNameSetting.Type                  =   SettingsType.Page;
            _baseSettings.AddSetting(EntityNameSetting);

            #endregion

			// CS 7/12/04 this is Added to manage Portal Settings extensions which could be necessary to some Package like E-Commerce
            PageSettingsCreatedEventArgs e	=	new PageSettingsCreatedEventArgs(_baseSettings,this);
			_baseSettings.OnPageSettingsCreated(e);
            if ( SettingsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageSettings GetPageBaseSettings END count: {0}", _baseSettings.Count),SettingsTraceSwitch.Sw.Info);
            return _baseSettings;
        }

        #endregion settings
    }
}
